<script type="text/javascript">
	var url;
	function newVisitor(){
		$('#dlg').dialog('open').dialog('setTitle','添加来访单位');
		$('#fm').form('clear');
		url = '<?php echo url('visitor','add')?>';
	}
	function editVisitor(){
		var row = $('#dg').datagrid('getSelected');
		if (row){
			$('#dlg').dialog('open').dialog('setTitle','编辑来访单位');
			$('#fm').form('load',row);
			url = '<?php echo url('visitor','update')?>&id='+row.id;
		}
	}
	function saveVisitor(){
		$('#fm').form('submit',{
			url: url,
			onSubmit: function(){
				return $(this).form('validate');
			},
			success: function(result){
				var result = eval('('+result+')');
				if (result.success){
					$('#dlg').dialog('close');		// close the dialog
					$('#dg').datagrid('reload');	// reload the user data
				} else {
					$.messager.show({
						title: '错误',
						msg: result.msg
					});
				}
			}
		});
	}
	function removeVisitor(){
		var row = $('#dg').datagrid('getSelected');
		if (row){
			$.messager.confirm('确认','一定要删除该来访单位吗?',function(r){
				if (r){
					$.post('<?php echo url('visitor','del')?>',{id:row.id},function(result){
						if (result.success){
							$('#dg').datagrid('reload');	// reload the user data
						} else {
							$.messager.show({	// show error message
								title: '错误',
								msg: result.msg
							});
						}
					},'json');
				}
			});
		}
	}
</script>

<table id="dg" title="来访单位管理" class="easyui-datagrid"
		url="<?php echo url('visitor','list')?>"
		toolbar="#toolbar" pagination="true"
		rownumbers="true" fitColumns="true" fit="true" singleSelect="true">
	<thead>
		<tr>
			<th field="name" width="50">来访单位名称</th>
		</tr>
	</thead>
</table>
<div id="toolbar">
	<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newVisitor()">添加来访单位</a>
	<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editVisitor()">编辑来访单位</a>
	<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="removeVisitor()">删除来访单位</a>
</div>

<div id="dlg" class="easyui-dialog" style="width:400px;height:400px;padding:10px 20px"
		closed="true" buttons="#dlg-buttons">
	<div class="ftitle">来访单位信息</div>
	<form id="fm" method="post" novalidate>
		<div class="fitem">
			<label>单位名称:</label>
			<input name="name" class="easyui-validatebox" required="true">
		</div>
	</form>
</div>
<div id="dlg-buttons">
	<a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveVisitor()">保存</a>
	<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>
</div>